package org.kernely.security.ui.user.mgmt;

import org.kernely.security.persistence.UserDetails;
import org.kernely.security.ui.SecurityUIBundle;
import org.kernely.ui.mvp.KernelyEventSender;
import org.kernely.ui.mvp.KernelyPresenter;

public class AddEditUserManagementPresenter extends KernelyPresenter<AddEditUserManagementView> implements KernelyEventSender{

	/**
	 * The UserDetails to edit in edition mode
	 */
	private UserDetails ud;
	private boolean editMode = false;

	/**
	 * Default constructor in creation mode
	 */
	public AddEditUserManagementPresenter(){}

	/**
	 * Constructor in edition mode
	 * @param ud : the UserDetails to edit
	 */
	public AddEditUserManagementPresenter(UserDetails ud){
		this.ud = ud;
		this.editMode = true;
	}

	/*
	 * (non-Javadoc)
	 * @see org.kernely.ui.mvp.KernelyPresenter#bind()
	 */
	@Override
	public final void bind() {
		// In edition mode, fill the fields with user's informations
		if(ud != null && editMode){
			this.view.getLogin().setValue(ud.getUser().getLogin());
			this.view.getName().setValue(ud.getName());
			this.view.getFirstname().setValue(ud.getFirstname());
			this.view.getMail().setValue(ud.getMail());
			this.view.getCivility().setValue(ud.getCivility());
		}
		
		if(editMode){
			view.getPassword().setRequired(false);
		}

	}
}
